<?php

namespace Admin\Controller;

use Admin\Model\ReturnOrderModel;

class ReturnAddressController extends CommonController
{

    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '订单';
        $this->breadcrumb2 = '退换货寄送地址';
    }

    public function index()
    {
        $where = array(
            'member_id' => '-1',
        );
        $address = M('address')->where($where)->find();

        if (IS_POST) {
            $data = I('post.');
            $data['member_id'] = '-1';
            $data['name'] = $data['name'];
            $data['telephone'] = $data['telephone'];
            $data['address'] = $data['address'];
            $data['default_address'] = '-1';
            if (empty($data['address_id'])) {
                $r = M('address')->add($data);
            } else {
                $where['address_id'] = $data['address_id'];
                $r = M('address')->where($where)->save($data);
            }

            if ($r) {
                $re['code'] = 200;
                $this->ajaxReturn($re);
                die;
            } else {
                $re['code'] = 400;
                $this->ajaxReturn($re);
                die;
            }
        }
        $this->assign('address', $address);
        $this->display();
    }

    function print_order()
    {
        $model = new ReturnOrderModel();

        $this->order = $model->order_info(I('id'));
        $this->print = true;
        $this->display('./Themes/Home/default/Mail/order.html');
    }

    public function show_order()
    {

        $this->crumbs = '订单详情';

        $model = new ReturnOrderModel();

        $this->data = $model->order_info(I('id'));

        $this->display('show');
    }

    function history()
    {
        $model = new ReturnOrderModel();

        if (IS_POST) {
            if (I('order_status_id') == C('cancel_order_status_id')) {
                $Order = new \Home\Model\ReturnOrderModel();
                $Order->cancel_order($_GET['id']);
                storage_user_action(session('user_auth.uid'), session('user_auth.username'), C('BACKEND_USER'), '取消了订单  ' . $_GET['id']);
                $result = true;
            } else {
                $result = $model->addOrderHistory($_GET['id'], $_POST);
            }

            /**
             * 判断是否选择了通知会员，并发送邮件
             */
            /*if(I('notify')==1){
                $order_info=M('order')->find($_GET['id']);

                $status=get_order_status_name(I('order_status_id'));

                $model=new \Admin\Model\ReturnOrderModel();
                $this->order=$model->order_info($_GET['id']);
                $this->seller_comment=$_POST['comment'];
                $html=$this->fetch('./Themes/Home/default/Mail/order.html');
                think_send_mail($order_info['email'],$order_info['name'],'订单-'.$status.'-'.C('SITE_NAME'),$html);
            }*/

            if ($result) {
                $this->success = '操作成功！！';
            } else {
                $this->error = '操作失败！！';
            }
        }

        //$results = $model->getOrderHistories($_GET['id']);

        /*foreach ($results as $result) {
            $histories[] = array(
                'notify'     => $result['notify'] ? '是' : '否',
                'status'     => $result['status'],
                'comment'    => nl2br($result['comment']),
                'date_added' => date('Y/m/d H:i:s', $result['date_added'])
            );
        }

        $this->histories=$histories;*/

        $this->display();
    }

    function del()
    {
        $model = new ReturnOrderModel();
        $return = $model->del_order(I('get.id'));
        $this->osc_alert($return);
    }

    function stag()
    {
        $order_id = I('get.id');
        //$model= M("Order");

        $sql = "select * from oscshop_staging where id = $order_id";
        $arr = M()->query($sql);
        $this->assign('arr', $arr);
        $this->display();
    }
}

?>